package com.bw.controller;

import com.bw.aop.MyLog;
import com.bw.domain.TGoods;
import com.bw.service.TGoodsService;
import com.bw.upload.FileUploadService;
import com.bw.utils.common.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.assertj.core.util.VisibleForTesting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author bobo棒
 * @date 2024年04月08日 15:36
 */

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    TGoodsService tGoodsService;
    @Autowired
    FileUploadService fileUploadService;



    @RequestMapping("del")
    public Result del( Integer id) throws IOException {
        boolean b = tGoodsService.removeById(id);
        return b?Result.success():Result.fail();
    }


    @RequestMapping("add")
    public Result add(@RequestBody TGoods tGoods) throws IOException {
        boolean b = tGoodsService.save(tGoods);
        return b?Result.success():Result.fail();
    }

    @RequestMapping("upFile")
    public Result upFile(MultipartFile file) throws IOException {
        String filename = fileUploadService.uploadFile(file);
        return Result.success(filename);
    }

    @MyLog
    @RequestMapping("list")
    public Result list(@RequestBody TGoods tGoods, @RequestParam(defaultValue = "1") Integer pageNum){

        PageHelper.startPage(pageNum,3);
        List<TGoods> list = tGoodsService.listGoods(tGoods);
        PageInfo<TGoods> info = new PageInfo<>(list);
        return Result.success(info);
    }

}
